using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MSRI.SmsService;
using MSRI.SmsSessions;
using MSRI.HelperClasses;
using System.Collections;


namespace SMSBackChannel
{
    public partial class Form1 : Form
    {
        public SmsServiceClass SmsService = null;
        public List<SmsClass> People;
        private delegate void UpdateCounterDelegate();
        int maxcount = 5;
        public Form1()
        {
            InitializeComponent();
            try
            {
                SmsService = new SmsServiceClass("SMSBackChannel");
                SmsService.OnSmsReceived += new OnSmsReceivedDelegate(SmsService_OnSmsReceived);
                // Use SmsService.SendSms(SmsClass Sms) to send a Sms
                People = new List<SmsClass>();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Application.Exit();
            }
        }

        SmsResponseClass SmsService_OnSmsReceived(SmsClass SmsMessage)
        {
            SmsResponseClass response = new SmsResponseClass();
            SmsClass SenderInfo = null;
            for (int i = 0; i < People.Count; i++) //check to see if we've already seen this person...
            {
                if (People[i].Number == SmsMessage.Number)
                {
                    SenderInfo = People[i];
                    break;
                }
            }
            if (SenderInfo == null && SmsMessage.Body.Trim().Length > 0)  //We have not seen this number before, so assume the body is their name...
            {
                People.Add(SmsMessage);
                SmsClass ResponseItem = new SmsClass();
                ResponseItem.Number = SmsMessage.Number;
                ResponseItem.Body = "Hi " + SmsMessage.Body + "!  Just reply to this message and your comment will be SMS'd to everyone else on the backchanel. Reply with 'stop' to leave.";
                response.AddResponseSms(ResponseItem);
                UpdateCounter();
            }
            else
            {
                if (SmsMessage.Body.Trim().ToLower() != "stop")
                {
                    int i = 0;
                    i = People.Count > maxcount ? People.Count - maxcount : 0;
                    for (; i < People.Count;i++)
                    {
                        SmsClass recipient = People[i];
                        if ((recipient.Number != SmsMessage.Number) && (recipient.Body != ""))
                        {
                            SmsClass ResponseItem = new SmsClass();
                            ResponseItem.Number = recipient.Number;
                            ResponseItem.Body = SenderInfo.Body + " says: " + SmsMessage.Body;
                            response.AddResponseSms(ResponseItem);
                        }
                    }
                    People.Remove(SenderInfo);
                    People.Add(SenderInfo);
                }
                else
                {  //they sent over "stop" so we should remove them...                    
                    SmsClass ResponseItem = new SmsClass();
                    ResponseItem.Number = SmsMessage.Number;
                    ResponseItem.Body = SenderInfo.Body + "!You've been removed from the backchannel. To rejoin, reply with your name.";
                    response.AddResponseSms(ResponseItem);
                    People.Remove(SenderInfo);
                    UpdateCounter();
                }
            }
            return response;
        }

        private void UpdateCounter()
        {
            if (InvokeRequired)
                Invoke(new UpdateCounterDelegate(UpdateCounter), new object[] { });
            else
                label2.Text = People.Count.ToString();
        }
    }
}
